﻿using System;
using System.Threading;
internal class Program
{
    static void Main(string[] args)
    {
        AlarmClock alarmClock = new AlarmClock();

        alarmClock.Tick += (sender, eventArgs) =>
        {
            Console.WriteLine("Tick...");
        };

        alarmClock.Alarm += (sender, eventArgs) =>
        {
            Console.WriteLine("Stop Alarm? (y/n)");
            string answer = Console.ReadLine();
            if (answer.ToLower() == "y")
            {
                alarmClock.StopAlarm();
            }
            else
            {
                Console.WriteLine("Please set alarm time:");
                Console.Write("Hour: ");
                int hour = int.Parse(Console.ReadLine());
                Console.Write("Minute: ");
                int minute = int.Parse(Console.ReadLine());
                Console.Write("Second: ");
                int second = int.Parse(Console.ReadLine());

                alarmClock.SetAlarm(hour, minute, second);
                alarmClock.Start();
            }
        };

        Console.WriteLine("Please set alarm time:");
        Console.Write("Hour: ");
        int hour; int minute; int second;
        while (true)
        {
            hour = int.Parse(Console.ReadLine());
            if (hour >= 0 && hour <= 24)
                break;
            Console.WriteLine("hour数据有误，请重新输入!");
            Console.Write("Hour: ");
        }

        Console.Write("Minute: ");
        while (true)
        {
            minute = int.Parse(Console.ReadLine());
            if (minute >= 0 && minute <= 60)
                break;
            Console.WriteLine("Minute数据有误，请重新输入!");
            Console.Write("Minute: ");

        }

        Console.Write("Second: ");
        while (true)
        {
            second = int.Parse(Console.ReadLine());
            if (minute >= 0 && minute <= 60)
                break;
            Console.WriteLine("Second数据有误，请重新输入!");
            Console.Write("Second: ");
        }
        alarmClock.SetAlarm(hour, minute, second);
        alarmClock.Start();
    }


    public class AlarmClock
    {
        public event EventHandler Tick;
        public event EventHandler Alarm;

        private int hour;
        private int minute;
        private int second;
        private int alarmHour;
        private int alarmMinute;
        private int alarmSecond;
        private bool alarmOn;

        public void SetAlarm(int hour, int minute, int second)
        {
            alarmHour = hour;
            alarmMinute = minute;
            alarmSecond = second;
            alarmOn = true;
        }

        public void Start()
        {
            while (true)
            {
                Thread.Sleep(1000);
                second++;
                if (second >= 60)
                {
                    second = 0;
                    minute++;
                    if (minute >= 60)
                    {
                        minute = 0;
                        hour++;
                        if (hour >= 24)
                        {
                            hour = 0;
                        }
                    }
                }
                Console.WriteLine($"Current time is {hour:D2}:{minute:D2}:{second:D2}");
                Tick?.Invoke(this, EventArgs.Empty);
                if (alarmOn && hour == alarmHour && minute == alarmMinute && second == alarmSecond)
                {
                    Console.WriteLine("Alarm!");
                    Alarm?.Invoke(this, EventArgs.Empty);
                    break;
                }
            }
        }

        public void StopAlarm()
        {
            alarmOn = false;
        }
    }

}
